//
// Created by work on 24-8-22.
//

#include "iot_model_device/device_model_mgr/CSsqlMgr.h"

bool CSsqlMgr::addUser(Json::Value user) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_user_name << "(";
    sql << userFieldName(USER_ENUM::EUserId) << ",";
    sql << userFieldName(USER_ENUM::EPwd) << ",";
    sql << userFieldName(USER_ENUM::ENickname) << ",";
    sql << userFieldName(USER_ENUM::EUtype) << ",";
    sql << userFieldName(USER_ENUM::EAdmin_usr) << ",";
    sql << userFieldName(USER_ENUM::EConfigs) << ",";
    sql << userFieldName(USER_ENUM::EMenu_permission) << ",";
    sql << userFieldName(USER_ENUM::EProperty_permission) << ",";
    sql << userFieldName(USER_ENUM::EGroupings) << ",";
    sql << userFieldName(USER_ENUM::ECreate_time) << ",";
    sql << userFieldName(USER_ENUM::EUpdate_time) << ",";
    sql << userFieldName(USER_ENUM::ELast_online_time) << ",";
    sql << userFieldName(USER_ENUM::ELast_login_time) << ",";
    sql << userFieldName(USER_ENUM::EMail_address) << ",";
    sql << userFieldName(USER_ENUM::EPhone_number);
    sql << ") VALUES (";
    sql << "'" << user[userFieldName(USER_ENUM::EUserId)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EPwd)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::ENickname)].asString() << "',";
    sql << user[userFieldName(USER_ENUM::EUtype)].asInt() << ",";
    sql << "'" << user[userFieldName(USER_ENUM::EAdmin_usr)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EConfigs)].toStyledString().c_str()<< "',";
    sql << "'" << user[userFieldName(USER_ENUM::EMenu_permission)].toStyledString().c_str() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EProperty_permission)].toStyledString().c_str() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EGroupings)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::ECreate_time)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EUpdate_time)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::ELast_online_time)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::ELast_login_time)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EMail_address)].asString() << "',";
    sql << "'" << user[userFieldName(USER_ENUM::EPhone_number)].asString() << "'";
    sql << ")";

    GET_DB_HANDLE(CConfigs::EPlat);
    if(!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec valid!" + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::delUser(const std::string &userid) {
    std::ostringstream sql;
    sql << "DELETE FROM " << t_user_name << " WHERE ";
    sql << userFieldName(USER_ENUM::EUserId) << "='" << userid << "'";
    GET_DB_HANDLE(CConfigs::EPlat);
    if(!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec valid!" + sql.str()));
        return false;
    }
    return true;
}

bool CSsqlMgr::changeUser(Json::Value user) {
    return false;
}

bool CSsqlMgr::getUser(const std::string &userid, Json::Value& user) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << userFieldName(USER_ENUM::EUserId) << ",";
    sql << userFieldName(USER_ENUM::EPwd) << ",";
    sql << userFieldName(USER_ENUM::ENickname) << ",";
    sql << userFieldName(USER_ENUM::EUtype) << ",";
    sql << userFieldName(USER_ENUM::EAdmin_usr) << ",";
    sql << userFieldName(USER_ENUM::EConfigs) << ",";
    sql << userFieldName(USER_ENUM::EMenu_permission) << ",";
    sql << userFieldName(USER_ENUM::EProperty_permission) << ",";
    sql << userFieldName(USER_ENUM::EGroupings) << ",";
    sql << userFieldName(USER_ENUM::ECreate_time) << ",";
    sql << userFieldName(USER_ENUM::EUpdate_time) << ",";
    sql << userFieldName(USER_ENUM::ELast_online_time) << ",";
    sql << userFieldName(USER_ENUM::ELast_login_time) << ",";
    sql << userFieldName(USER_ENUM::EMail_address) << ",";
    sql << userFieldName(USER_ENUM::EPhone_number);
    sql << " FROM " << t_user_name << " WHERE ";
    sql << userFieldName(USER_ENUM::EUserId) << "='" << userid << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || !res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    user[userFieldName(USER_ENUM::EUserId)] = res->asString(0);
    user[userFieldName(USER_ENUM::EPwd)] = res->asString(1);
    user[userFieldName(USER_ENUM::ENickname)] = res->asString(2);
    user[userFieldName(USER_ENUM::EUtype)] = res->asInt(3);
    user[userFieldName(USER_ENUM::EAdmin_usr)] = res->asString(4);

    Json::Value root;
    std::string jsonString = res->asString(5);
    std::string errors;
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    user[userFieldName(USER_ENUM::EConfigs)] = reader->parse(jsonString.c_str(), jsonString.c_str() + jsonString.length(), &root, &errors) ? root : Json::Value(Json::nullValue);

    Json::Value meonroot;
    std::string meonString = res->asString(6);
    std::string meonerror;
    Json::CharReaderBuilder buildermeon;
    const std::unique_ptr<Json::CharReader> readermeon(buildermeon.newCharReader());
    user[userFieldName(USER_ENUM::EMenu_permission)] = readermeon->parse(meonString.c_str(), meonString.c_str() + meonString.length(), &meonroot, &meonerror) ? meonroot : Json::Value(Json::nullValue);

    Json::Value rootpro;
    std::string jsonStringpro = res->asString(7);
    std::string errorspro;
    Json::CharReaderBuilder builderpro;
    const std::unique_ptr<Json::CharReader> readerpro(builderpro.newCharReader());
    user[userFieldName(USER_ENUM::EProperty_permission)] = readerpro->parse(jsonStringpro.c_str(), jsonStringpro.c_str() + jsonStringpro.length(), &rootpro, &errorspro) ? rootpro : Json::Value(Json::nullValue);

    user[userFieldName(USER_ENUM::EGroupings)] = res->asString(8);
    user[userFieldName(USER_ENUM::ECreate_time)] = res->asString(9);
    user[userFieldName(USER_ENUM::EUpdate_time)] = res->asString(10);
    user[userFieldName(USER_ENUM::ELast_online_time)] = res->asString(11);
    user[userFieldName(USER_ENUM::ELast_login_time)] = res->asString(12);
    user[userFieldName(USER_ENUM::EMail_address)] = res->asString(13);
    user[userFieldName(USER_ENUM::EPhone_number)] = res->asString(14);
    return true;
}

bool CSsqlMgr::checkUserNoExist(const std::string& userid) {
    std::ostringstream sql;
    sql << "SELECT * FROM " << t_user_name << " WHERE ";
    sql << userFieldName(USER_ENUM::EUserId) << "='" << userid << "'";
    CConfigs::SDataBaseCfg cfg = CConfigs::ins().dateBaseCfg( CConfigs::EPlat );
    std::shared_ptr<iotplat::CDb> db(createDb(cfg.lib.c_str(), cfg.ip.c_str(), cfg.engine.c_str(),cfg.port));
    if(!db->connect(cfg.username.c_str(), cfg.password.c_str())) {
        std::cout << "db name:" << cfg.lib.c_str() << std::endl;
        std::cout << "ip:" << cfg.ip.c_str() << std::endl;
        std::cout << "port:" << cfg.port << std::endl;
        std::cout << "ip:" << cfg.ip.c_str() << std::endl;
        std::cout << "user:" << cfg.username.c_str() << std::endl;
        std::cout << "pwd:" << cfg.password.c_str() << std::endl;
        THROW_ERROR_WITH_INFO("db connect error!");
    }
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

[[maybe_unused]] bool CSsqlMgr::addModel(const CModelInfo& model) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_model_name << "(";
    sql << mdoelFieldName(MODEL_ENUM_NAME) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_USER) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_DESCR) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_SN) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_GROUPINGS) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_LOCATION) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_CREATE_TIME) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_UPDATE_TIME) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_PRODUCER) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_VALID) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_CONFIGS);
    sql << ") VALUES (";
    sql << "'" << model.name << "', ";
    sql << "'" << model.user << "', ";
    sql << "'" << model.descr << "', ";
    sql << model.sn << ", ";
    sql << "'" << model.grouping << "', ";
    sql << "'" << model.location << "', ";
    sql << "'" << model.create_time.ToString() << "', ";
    sql << "'" << model.update_time.ToString() << "', ";
    sql << "'" << model.producer << "', ";
    sql << BOOL_TO_STRING(model.valid) << ",";
    sql << "'" << model.configs.toStyledString().c_str() << "'";
    sql << ")";

    GET_DB_HANDLE(CConfigs::EPlat);
    if(!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec valid!" + sql.str()));
    }

    return true;
}

[[maybe_unused]] bool CSsqlMgr::delModel(const std::string& userid, const std::string& name) {

    GET_DB_HANDLE(CConfigs::EPlat);
    db->exec("START TRANSACTION;");

    std::vector<std::string> t_names = {t_yx_name, t_yc_name, t_yk_name, t_parameter_name};
    for(std::string t_name : t_names) {
        std::ostringstream delsql;
        delsql << "DELETE FROM " << t_name << " WHERE ";

        delsql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << userid << "'";
        delsql << " AND ";
        delsql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << name << "'";

        if (!db->exec(delsql.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string(userid+"."+name+"delete all"+t_name+ "failed: " + delsql.str()));
            db->exec("ROLLBACK;");
            return false;
        }
    }

    std::ostringstream sql;
    sql << "DELETE FROM " << t_model_name << " WHERE ";
    std::string userIdColumnName = mdoelFieldName(MODEL_ENUM_USER);
    std::string nameColumnName = mdoelFieldName(MODEL_ENUM_NAME);

    sql << userIdColumnName << " = '" << userid << "'";
    sql << " AND " << nameColumnName << " = '" << name << "'";

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        db->exec("ROLLBACK;");
        return false;
    }

    db->exec("COMMIT;");
    return true;
}

[[maybe_unused]] bool CSsqlMgr::changeModel(const CModelInfo& model) {
    std::ostringstream sql;
    sql << "UPDATE " << t_model_name << " SET ";
    sql << mdoelFieldName(MODEL_ENUM::EName) << "='" << model.name << "',";
    sql << mdoelFieldName(MODEL_ENUM::EUser) << "='" << model.user << "',";
    sql << mdoelFieldName(MODEL_ENUM::EDescr) << "='" << model.descr << "',";
    sql << mdoelFieldName(MODEL_ENUM::ESn) << "=" << std::to_string(model.sn) << ",";
    sql << mdoelFieldName(MODEL_ENUM::EGroupIngs) << "='" << model.grouping << "',";
    sql << mdoelFieldName(MODEL_ENUM::ELocation) << "='" << model.location << "',";
    sql << mdoelFieldName(MODEL_ENUM::ECreate_time) << "='" << model.create_time.ToString() << "',";
    sql << mdoelFieldName(MODEL_ENUM::EUpdate_time) << "='" << model.update_time.ToString() << "',";
    sql << mdoelFieldName(MODEL_ENUM::EProducer) << "='" << model.producer << "',";
    sql << mdoelFieldName(MODEL_ENUM::EValid) << "=" << BOOL_TO_STRING(model.valid) << ",";
    sql << mdoelFieldName(MODEL_ENUM::EConfigs) << "='" << model.configs.toStyledString().c_str() << "'";
    sql << " WHERE " << mdoelFieldName(MODEL_ENUM::EUser) << " = '" << model.user << "'";
    sql << " AND " << mdoelFieldName(MODEL_ENUM::EName) << " = '" << model.name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

[[maybe_unused]] bool CSsqlMgr::getModel(const std::string& userid, const std::string& name, CModelInfo& model) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << mdoelFieldName(MODEL_ENUM_NAME) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_USER) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_DESCR) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_SN) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_GROUPINGS) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_LOCATION) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_CREATE_TIME) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_UPDATE_TIME) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_PRODUCER) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_VALID) << ", ";
    sql << mdoelFieldName(MODEL_ENUM_CONFIGS);
    sql << " FROM " << t_model_name << " WHERE ";
    sql << mdoelFieldName(MODEL_ENUM_USER) << " = '" << userid << "'";
    sql << " AND ";
    sql << mdoelFieldName(MODEL_ENUM_NAME) << " = '" << name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));
    if(!res || !res->next())
        return false;

    model.name = res->asString(0);
    model.user = res->asString(1);
    model.descr = res->asString(2);
    model.sn = res->asInt(3);
    model.grouping = res->asString(4);
    model.location = res->asString(5);
    model.create_time = CDateTime::FromString(res->asString(6));
    model.update_time = CDateTime::FromString(res->asString(7));
    model.producer = res->asString(8);
    model.valid = res->asBool(9);

    Json::Value root;
    std::string jsonString = res->asString(10);
    std::string errors;
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    model.configs = reader->parse(jsonString.c_str(), jsonString.c_str() + jsonString.length(), &root, &errors) ? root : Json::Value(Json::nullValue);

    return true;
}

bool CSsqlMgr::getModels(const std::string &userid, std::vector<CModelInfo> &models) {
    std::ostringstream sql;
    sql << "SELECT " << mdoelFieldName(MODEL_ENUM::EName) << " FROM " << t_model_name;
    sql << " WHERE " << mdoelFieldName(MODEL_ENUM::EUser) << "='" << userid << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> indexsRes(db->query(sql.str().c_str()));
    if(!indexsRes)
        return false;

    std::vector<std::string> names;
    while (indexsRes->next()) {
        names.push_back(indexsRes->asString(0));
    }

    for(std::string name : names) {
        CModelInfo model;
        if(!getModel(userid, name, model)) {
            return false;
        }
        models.push_back(model);
    }
    return true;
}

bool CSsqlMgr::addyx(const CRtModelYxInfo& yx) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_yx_name << "(";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ", ";
    sql << yxFieldName(YX_ENUM::EValues) << ")";
    sql << " VALUES (";
    sql << "'" << yx.model_name << "', ";
    sql << "'" << yx.user << "', ";
    sql << "'" << yx.name << "', ";
    sql << "'" << yx.descr << "', ";
    sql << yx.sn << ", ";
    sql << "'" << yx.grouping << "', ";
    sql << "'" << yx.configs.toStyledString().c_str() << "', ";

    Json::Value valuesobj;
    Json::Value tmp = yx.valuesToJson();
    for(auto &obj : tmp) {
        std::string desc = obj["v_descr"].asString();
        std::string res =  toEscapedUnicode(desc);
        obj["v_descr"] = res;
    }
    valuesobj["valuesmap"] = tmp;
    sql << "'" << valuesobj.toStyledString().c_str() << "')";

    GET_DB_HANDLE(CConfigs::EPlat);
    if(!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("数据库执行添加遥信失败!")+sql.str());
        return false;
    }

    return true;
}

bool CSsqlMgr::delYx(const std::string &userid, const std::string &modelname, const std::string &name) {
    return delSig(userid, modelname, name, t_yk_name);
}

bool CSsqlMgr::changeYx(const CRtModelYxInfo& yx) {
    std::ostringstream sql;
    sql << "UPDATE " << t_yx_name << " SET ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << "='" << yx.descr <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << "=" << yx.sn <<",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << "='" << yx.grouping <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << "='" << yx.configs.toStyledString().c_str() <<"',";
    Json::Value valuesobj;
    valuesobj[yxFieldName(YX_ENUM::EValues)] = yx.valuesToJson();
    sql << yxFieldName(YX_ENUM::EValues) << "='" << valuesobj.toStyledString() <<"',";
    sql << " WHERE ";

    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << yx.user << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << yx.model_name << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << yx.name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::getYx(const std::string &userid, const std::string &modelname, const std::string &name, CRtModelYxInfo& yx) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ",";
    sql << yxFieldName(YX_ENUM::EValues);
    sql << " FROM " << t_yx_name << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << modelname << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || !res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    yx.model_name = res->asString(0);
    yx.user = res->asString(1);
    yx.name = res->asString(2);
    yx.descr = res->asString(3);
    yx.sn = res->asInt(4);
    yx.grouping = res->asString(5);
    yx.configs = stringToJson(res->asString(6));
    Json::Value valuesobj = stringToJson(res->asString(7));
    yx.valuesFromJson(valuesobj[yxFieldName(YX_ENUM::EValues)]);
    return true;
}

bool CSsqlMgr::delSig(const std::string &userid, const std::string &modelname, const std::string &name,
                      const std::string &table) {
    std::ostringstream sql;

    sql << "DELETE FROM " << table << " WHERE ";

    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << modelname << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::getYxs(const std::string &userid, const std::string &modelname, std::vector<CRtModelYxInfo> &yxs) {
    std::ostringstream sql;
    sql << "SELECT " << baseSigFieldName(BASE_SIG_ENUM::EName) << " FROM " << t_yx_name;
    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << "='" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << "='" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res)
        return false;

    std::vector<std::string> names;
    while (res->next()){
        names.push_back(res->asString(0));
    }

    for(std::string name : names) {
        CRtModelYxInfo info;
        if(!getYx(userid, modelname, name, info))
            return false;

        yxs.push_back(info);
    }
    return true;
}

bool CSsqlMgr::addyc(const CRtModelYcInfo& yc) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_yc_name << "(";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ", ";
    sql << ycFieldName(YC_ENUM::EBase) << ", ";
    sql << ycFieldName(YC_ENUM::ECoef) << ", ";
    sql << ycFieldName(YC_ENUM::EEn_alarm) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_up_limit) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_down_limit) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_up_limit_desc) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_down_limit_desc) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_time_count);
    sql << ") VALUES (";
    sql << "'" << yc.model_name << "', ";
    sql << "'" << yc.user << "', ";
    sql << "'" << yc.name << "', ";
    sql << "'" << yc.descr << "', ";
    sql << std::to_string(yc.sn) << ", ";
    sql << "'" << yc.grouping << "', ";
    sql << "'" << yc.configs.toStyledString().c_str() << "', ";
    sql << std::to_string(yc.base) << ",";
    sql << std::to_string(yc.coef) << ",";
    sql << BOOL_TO_STRING(yc.en_alarm)  << ",";
    sql << std::to_string(yc.alarm_up_limit) << ",";
    sql << std::to_string(yc.alarm_down_limit) << ",";
    sql << "'" << yc.alarm_up_limit_desc << "', ";
    sql << "'" << yc.alarm_down_limit_desc << "', ";
    sql << std::to_string(yc.alarm_time_count) << ")";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::delYc(const std::string &userid, const std::string &modelname, const std::string &name) {
    return delSig(userid, modelname, name, t_yc_name);
}

bool CSsqlMgr::changeYc(const CRtModelYcInfo& yc) {
    std::ostringstream sql;
    sql << "UPDATE " << t_yc_name << " SET ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << "='" << yc.descr <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << "=" << std::to_string(yc.sn) <<",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << "='" << yc.grouping <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << "='" << yc.configs.toStyledString().c_str() <<"',";
    sql << ycFieldName(YC_ENUM::EBase) << "=" << std::to_string(yc.base) <<",";
    sql << ycFieldName(YC_ENUM::ECoef) << "=" << std::to_string(yc.coef) <<",";
    sql << ycFieldName(YC_ENUM::EEn_alarm) << "=" << BOOL_TO_STRING(yc.en_alarm) <<",";
    sql << ycFieldName(YC_ENUM::EAlarm_up_limit) << "=" << std::to_string(yc.alarm_up_limit) <<",";
    sql << ycFieldName(YC_ENUM::EAlarm_down_limit) << "=" << std::to_string(yc.alarm_down_limit) <<",";
    sql << ycFieldName(YC_ENUM::EAlarm_up_limit_desc) << "='" << yc.alarm_up_limit_desc <<"',";
    sql << ycFieldName(YC_ENUM::EAlarm_down_limit_desc) << "='" << yc.alarm_down_limit_desc <<"',";
    sql << ycFieldName(YC_ENUM::EAlarm_time_count) << "=" << std::to_string(yc.alarm_time_count);
    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << yc.user << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << yc.model_name << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << yc.name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool
CSsqlMgr::getYc(const std::string &userid, const std::string &modelname, const std::string &name, CRtModelYcInfo& yc) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ",";
    sql << ycFieldName(YC_ENUM::EBase) << ", ";
    sql << ycFieldName(YC_ENUM::ECoef) << ", ";
    sql << ycFieldName(YC_ENUM::EEn_alarm) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_up_limit) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_down_limit) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_up_limit_desc) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_down_limit_desc) << ", ";
    sql << ycFieldName(YC_ENUM::EAlarm_time_count);
    sql << " FROM " << t_yc_name << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << modelname << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || !res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    yc.model_name = res->asString(0);
    yc.user = res->asString(1);
    yc.name = res->asString(2);
    yc.descr = res->asString(3);
    yc.sn = res->asInt(4);
    yc.grouping = res->asString(5);
    Json::Value root;
    std::string jsonString = res->asString(6);
    std::string errors;
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    yc.configs = reader->parse(jsonString.c_str(), jsonString.c_str() + jsonString.length(), &root, &errors) ? root : Json::Value(Json::nullValue);
    yc.base = static_cast<float>(res->asDouble(7));
    yc.coef = static_cast<float>(res->asDouble(8));
    yc.en_alarm = res->asBool(9);
    yc.alarm_up_limit = static_cast<float>(res->asDouble(10));
    yc.alarm_down_limit = static_cast<float>(res->asDouble(11));
    yc.alarm_up_limit_desc = res->asString(12);
    yc.alarm_down_limit_desc = res->asString(13);
    yc.alarm_time_count = res->asInt(14);
    return true;
}

bool CSsqlMgr::getYcs(const std::string &userid, const std::string &modelname, std::vector<CRtModelYcInfo> &ycs) {
    std::ostringstream sql;
    sql << "SELECT " << baseSigFieldName(BASE_SIG_ENUM::EName) << " FROM " << t_yc_name;
    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << "='" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << "='" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res)
        return false;

    std::vector<std::string> names;
    while (res->next()){
        names.push_back(res->asString(0));
    }

    for(std::string name : names) {
        CRtModelYcInfo info;
        if(!getYc(userid, modelname, name, info))
            return false;

        ycs.push_back(info);
    }
    return true;
}

bool CSsqlMgr::addyk(const CRtModelYkInfo& yk) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_yk_name << "(";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ", ";
    sql << ykFieldName(YK_ENUM::EEn_write) << ", ";
    sql << ykFieldName(YK_ENUM::EValues);
    sql << ") VALUES (";
    sql << "'" << yk.model_name << "', ";
    sql << "'" << yk.user << "', ";
    sql << "'" << yk.name << "', ";
    sql << "'" << yk.descr << "', ";
    sql << std::to_string(yk.sn) << ", ";
    sql << "'" << yk.grouping << "', ";
    sql << "'" << yk.configs.toStyledString().c_str() << "', ";
    sql << BOOL_TO_STRING(yk.en_write) << ", ";

    Json::Value obj;
    obj[ykFieldName(YK_ENUM::EValues)] = yk.valuesToJson();
    sql << "'" << obj.toStyledString().c_str() << "')";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::delYk(const std::string &userid, const std::string &modelname, const std::string &name) {
    return delSig(userid, modelname, name, t_yk_name);
}

bool CSsqlMgr::changeYk(const CRtModelYkInfo& yk) {
    std::ostringstream sql;
    sql << "UPDATE " << t_yc_name << " SET ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << "='" << yk.descr <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << "=" << std::to_string(yk.sn) <<",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << "='" << yk.grouping <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << "='" << yk.configs.toStyledString().c_str() <<"',";
    sql << ykFieldName(YK_ENUM::EEn_write) << "=" << BOOL_TO_STRING(yk.en_write) <<",";

    Json::Value obj;
    obj[ykFieldName(YK_ENUM::EValues)] = yk.valuesToJson();
    sql << ykFieldName(YK_ENUM::EValues) << "='" << obj.toStyledString().c_str() <<"'";

    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << yk.user << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << yk.model_name << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << yk.name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::getYk(const std::string &userid, const std::string &modelname, const std::string &name, CRtModelYkInfo& yk) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ",";
    sql << ykFieldName(YK_ENUM::EEn_write) << ", ";
    sql << ykFieldName(YK_ENUM::EValues);
    sql << " FROM " << t_yk_name << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << modelname << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || !res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    yk.model_name = res->asString(0);
    yk.user = res->asString(1);
    yk.name = res->asString(2);
    yk.descr = res->asString(3);
    yk.sn = res->asInt(4);
    yk.grouping = res->asString(5);
    yk.configs = stringToJson(res->asString(6));
    yk.en_write = res->asBool(7);

    Json::Value obj = stringToJson(res->asString(8));
    yk.valuesFromJson(obj[ykFieldName(YK_ENUM::EValues)]);

    return true;
}

bool CSsqlMgr::getYks(const std::string &userid, const std::string &modelname, std::vector<CRtModelYkInfo> &yks) {
    std::ostringstream sql;
    sql << "SELECT " << baseSigFieldName(BASE_SIG_ENUM::EName) << " FROM " << t_yk_name;
    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << "='" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << "='" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res)
        return false;

    std::vector<std::string> names;
    while (res->next()){
        names.push_back(res->asString(0));
    }

    for(std::string name : names) {
        CRtModelYkInfo info;
        if(!getYk(userid, modelname, name, info))
            return false;

        yks.push_back(info);
    }
    return true;
}

bool CSsqlMgr::addParameter(const CRtModelParameterInfo& parameter) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_parameter_name << "(";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ", ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ", ";
    sql << parameterFieldName(PARAMETER_ENUM::EPtype) << ", ";
    sql << parameterFieldName(PARAMETER_ENUM::EEn_write) << ", ";
    sql << parameterFieldName(PARAMETER_ENUM::EEn_read);
    sql << ") VALUES (";
    sql << "'" << parameter.model_name << "', ";
    sql << "'" << parameter.user << "', ";
    sql << "'" << parameter.name << "', ";
    sql << "'" << parameter.descr << "', ";
    sql << std::to_string(parameter.sn) << ", ";
    sql << "'" << parameter.grouping << "', ";
    sql << "'" << parameter.configs.toStyledString().c_str() << "', ";
    sql << std::to_string(parameter.ptype) << ", ";
    sql << BOOL_TO_STRING(parameter.en_write) << ",";
    sql << BOOL_TO_STRING(parameter.en_read) << ")";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::delParameter(const std::string &userid, const std::string &modelname, const std::string &name) {
    return delSig(userid, modelname, name, t_parameter_name);
}

bool CSsqlMgr::changeParameter(const CRtModelParameterInfo& parameter) {
    std::ostringstream sql;
    sql << "UPDATE " << t_parameter_name << " SET ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << "='" << parameter.descr <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << "=" << std::to_string(parameter.sn) <<",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << "='" << parameter.grouping <<"',";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << "='" << parameter.configs.toStyledString().c_str() <<"',";
    sql << parameterFieldName(PARAMETER_ENUM::EPtype) << "=" << std::to_string(parameter.ptype) << ",";
    sql << parameterFieldName(PARAMETER_ENUM::EEn_write) << "=" << BOOL_TO_STRING(parameter.en_write) << ",";
    sql << parameterFieldName(PARAMETER_ENUM::EEn_read) << "=" << BOOL_TO_STRING(parameter.en_read);
    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << parameter.user << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << parameter.model_name << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << parameter.name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }
    return true;
}

bool CSsqlMgr::getParameter(const std::string &userid, const std::string &modelname, const std::string &name,
                            CRtModelParameterInfo& parameter) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EDescr) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::ESn) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EGrouping) << ",";
    sql << baseSigFieldName(BASE_SIG_ENUM::EConfigs) << ",";
    sql << parameterFieldName(PARAMETER_ENUM::EPtype) << ", ";
    sql << parameterFieldName(PARAMETER_ENUM::EEn_write) << ", ";
    sql << parameterFieldName(PARAMETER_ENUM::EEn_read);
    sql << " FROM " << t_parameter_name << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << " = '" << modelname << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EName) << " = '" << name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || !res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    parameter.model_name = res->asString(0);
    parameter.user = res->asString(1);
    parameter.name = res->asString(2);
    parameter.descr = res->asString(3);
    parameter.sn = res->asInt(4);
    parameter.grouping = res->asString(5);
    Json::Value root;
    std::string jsonString = res->asString(6);
    std::string errors;
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    parameter.configs = reader->parse(jsonString.c_str(), jsonString.c_str() + jsonString.length(), &root, &errors) ? root : Json::Value(Json::nullValue);
    parameter.ptype = static_cast<CRtModelParameterInfo::EParameterType>(res->asInt(7));
    parameter.en_write = res->asBool(8);
    parameter.en_read = res->asBool(9);
    return true;
}

bool CSsqlMgr::getParameters(const std::string &userid, const std::string &modelname,
                             std::vector<CRtModelParameterInfo> &parameters) {
    std::ostringstream sql;
    sql << "SELECT " << baseSigFieldName(BASE_SIG_ENUM::EName) << " FROM " << t_parameter_name;
    sql << " WHERE ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EUser) << "='" << userid << "'";
    sql << " AND ";
    sql << baseSigFieldName(BASE_SIG_ENUM::EModel_name) << "='" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res)
        return false;

    std::vector<std::string> names;
    while (res->next()){
        names.push_back(res->asString(0));
    }

    for(std::string name : names) {
        CRtModelParameterInfo info;
        if(!getParameter(userid, modelname, name, info))
            return false;

        parameters.push_back(info);
    }
    return true;
}

bool CSsqlMgr::addDevice(CRtDeviceInfo &device) {
    std::ostringstream sql;
    sql << "INSERT INTO " << t_device_name << "(";
    sql << deviceFieldName(DEVICE_ENUM::EUser) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EName) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EModelName) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EDescr) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ESn) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EGroupIngs) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EConfigs) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ELocation) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ECreate_time) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EProtocol) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EFirmwareVersion) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ELastOnlineTime) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EAccruedOnlineTime) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ESpecifications) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EValid) << ")";
    sql << " VALUES (";
    sql << "'" << device.user << "', ";
    sql << "'" << device.name << "', ";
    sql << "'" << device.model_name << "', ";
    sql << "'" << device.descr << "', ";
    sql << std::to_string(device.sn) << ", ";
    sql << "'" << device.grouping << "', ";
    sql << "'" << device.configs.toStyledString().c_str() << "', ";
    sql << "'" << device.location << "', ";
    sql << "'" << device.create_time.ToString().c_str() << "', ";
    sql << "'" << device.protocol << "', ";
    sql << "'" << device.firmware_version << "', ";
    sql << "'" << device.last_online_time.ToString().c_str() << "', ";
    sql << std::to_string(device.accrued_online_time) << ", ";
    sql << "'" << device.specifications << "', ";
    sql << "'" << BOOL_TO_STRING(device.valid) << "')";

    GET_DB_HANDLE(CConfigs::EPlat);
    if(!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO("数据库执行添加设备失败!");
        return false;
    }

    return true;
}

bool CSsqlMgr::delDevice(const std::string &userid, const std::string &modelname, const std::string &name) {
    std::ostringstream sql;

    sql << "DELETE FROM " << t_device_name << " WHERE ";

    sql << deviceFieldName(DEVICE_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EModelName) << " = '" << modelname << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EName) << " = '" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::changeDevice(const CRtDeviceInfo &device) {
    std::ostringstream sql;
    sql << "UPDATE " << t_device_name << " SET ";
    sql << deviceFieldName(DEVICE_ENUM::EDescr) << "='" << device.descr << "',";
    sql << deviceFieldName(DEVICE_ENUM::ESn) << "=" << std::to_string(device.sn) << ",";
    sql << deviceFieldName(DEVICE_ENUM::EGroupIngs) << "='" << device.grouping << "',";
    sql << deviceFieldName(DEVICE_ENUM::EConfigs) << "='" << device.configs.toStyledString().c_str() << "',";
    sql << deviceFieldName(DEVICE_ENUM::ELocation) << "='" << device.location << "',";
    sql << deviceFieldName(DEVICE_ENUM::ECreate_time) << "='" << device.create_time.ToString().c_str() << "',";
    sql << deviceFieldName(DEVICE_ENUM::EProtocol) << "='" << device.protocol << "',";
    sql << deviceFieldName(DEVICE_ENUM::EFirmwareVersion) << "='" << device.firmware_version << "',";
    sql << deviceFieldName(DEVICE_ENUM::ELastOnlineTime) << "='" << device.last_online_time.ToString().c_str() << "',";
    sql << deviceFieldName(DEVICE_ENUM::EAccruedOnlineTime) << "='" << std::to_string(device.accrued_online_time) << "',";
    sql << deviceFieldName(DEVICE_ENUM::ESpecifications) << "='" << device.specifications << "',";
    sql << deviceFieldName(DEVICE_ENUM::EValid) << "=" << BOOL_TO_STRING(device.valid);

    sql << " WHERE ";

    sql << deviceFieldName(DEVICE_ENUM::EUser) << " = '" << device.user << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EModelName) << " = '" << device.model_name << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EName) << " = '" << device.name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);

    if (!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    return true;
}

bool CSsqlMgr::getDevice(const std::string &userid, const std::string &modelname, const std::string &name,
    CRtDeviceInfo &device) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << deviceFieldName(DEVICE_ENUM::EUser) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EName) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EModelName) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EDescr) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ESn) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EGroupIngs) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EConfigs) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ELocation) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ECreate_time) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EProtocol) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EFirmwareVersion) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ELastOnlineTime) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EAccruedOnlineTime) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::ESpecifications) << ", ";
    sql << deviceFieldName(DEVICE_ENUM::EValid);
    sql << " FROM " << t_device_name << " WHERE ";
    sql << deviceFieldName(DEVICE_ENUM::EUser) << " = '" << userid << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EModelName) << " = '" << modelname << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EName) << " = '" << name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res || !res->next()){
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    device.user = res->asString(0);
    device.name = res->asString(1);
    device.model_name = res->asString(2);
    device.descr = res->asString(3);
    device.sn = res->asInt(4);
    device.grouping = res->asString(5);

    Json::Value root;
    std::string jsonString = res->asString(6);
    std::string errors;
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    device.configs = reader->parse(jsonString.c_str(), jsonString.c_str() + jsonString.length(), &root, &errors) ? root : Json::Value(Json::nullValue);

    device.location = res->asString(7);
    device.create_time = CDateTime::FromString(res->asString(8));
    device.protocol = res->asString(9);
    device.firmware_version = res->asString(10);
    device.last_online_time = CDateTime::FromString(res->asString(11));
    device.accrued_online_time = res->asInt(12);
    device.specifications = res->asString(13);
    device.valid = res->asBool(14);

    device.pulse = 1800;
    device.login_time = CDateTime::Now();
    device.online_state = false;
    return true;
}

bool CSsqlMgr::getDevices(const std::string &userid, const std::string& modelname, std::vector<CRtDeviceInfo> &devices) {
    std::ostringstream sql;
    sql << "SELECT ";
    sql << deviceFieldName(DEVICE_ENUM::EName);
    sql << " FROM " << t_device_name;
    sql << " WHERE ";
    sql << deviceFieldName(DEVICE_ENUM::EUser) << "='" << userid << "'";
    sql << " AND ";
    sql << deviceFieldName(DEVICE_ENUM::EModelName) << "='" << modelname << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));

    if(!res)
        return false;

    std::vector<std::string> devNames;
    while (res->next()){
        devNames.push_back(res->asString(0));
    }

    for(std::string& devname : devNames) {
        CRtDeviceInfo info;
        if(!getDevice(userid, modelname, devname, info))
            return false;
        devices.push_back(info);
    }
    return true;
}

bool CSsqlMgr::getModbusMaps(const std::string &userid, const std::string &model_name, Json::Value &modbusMaps) {
    std::ostringstream sql;
    sql << "SELECT map_id,userid,model_name,descr,create_time FROM t_modbus_map";
    sql << " WHERE ";
    sql << "userid='" << userid << "'";
    sql << " AND ";
    sql << "model_name='" << model_name << "'";

    GET_DB_HANDLE(CConfigs::EPlat);
    std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));
    if(!res) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }

    Json::Value maparr(Json::arrayValue);
    while(res->next()) {
        Json::Value map;
        map["map_id"] = res->asString(0);
        map["userid"] = res->asInt(1);
        map["model_name"] = res->asString(2);
        map["descr"] = res->asString(3);
        map["create_time"] = res->asString(4);
        maparr.append(map);
    }
    modbusMaps["maps"] = maparr;
    return true;
}

bool CSsqlMgr::addModbusMap(Json::Value &map) {
    std::ostringstream sql;
    sql << "INSERT INTO t_modbus_map VALUES (";
    sql << "'" << map["map_id"].asString() << "',";
    sql << "'" << map["userid"].asString() << "',";
    sql << "'" << map["model_name"].asString() << "',";
    sql << "'" << map["descr"].asString() << "',";
    sql << "'" << map["create_time"].asString() << "')";
    GET_DB_HANDLE(CConfigs::EPlat);
    if(!db->exec(sql.str().c_str())) {
        OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
        return false;
    }
    return true;
}

bool CSsqlMgr::delModbusMap(const std::string &map_id) {
    return true;
}

bool CSsqlMgr::addModbusMapYxs(const std::string &map_id, Json::Value &mapyxs) {
    GET_DB_HANDLE(CConfigs::EPlat);

    for(auto& item : mapyxs) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        bool is_filter = item["is_filter"].asBool();
        bool is_bit = item["is_bit"].asBool();
        int bit_offest = item["bit_offset"].asInt();
        int bit_num = item["bit_num"].asInt();

        buf << "INSERT INTO t_modbus_map_yx(map_id,name,reg_type,address,is_bit,bit_offset,bit_num,is_filter) VALUES (";
        buf << "'" << map_id << "',";
        buf << "'" << name << "',";
        buf  << reg_type << ",";
        buf << address << ",";
        buf << is_bit << ",";
        buf << bit_offest << ",";
        buf << bit_num << ",";
        buf << is_filter << ");";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }

    return true;
}

bool CSsqlMgr::changeModbusMapYxs(const std::string &map_id, Json::Value &mapyxs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapyxs) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        bool is_filter = item["is_filter"].asBool();
        bool is_bit = item["is_bit"].asBool();
        int bit_offest = item["bit_offset"].asInt();
        int bit_num = item["bit_num"].asInt();

        buf << "UPDATE t_modbus_map_yx SET ";
        buf << "reg_type=" << reg_type <<",";
        buf << "address=" << address << ",";
        buf << "is_bit=" << is_bit << ",";
        buf << "is_filter=" << is_filter << ",";
        buf << "bit_offset=" << bit_offest << ",";
        buf << "bit_num=" << bit_num;
        buf << " WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::deleteModbusMapYxs(const std::string &map_id, Json::Value &mapyxs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapyxs) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        buf << "DELETE FROM t_modbus_map_yx WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";
        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::getModbusMapYxs(const std::string &map_id, Json::Value &mapsigs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    Json::Value& yxs = mapsigs["mapsigs"];
    for(auto& yx : yxs) {
        std::string name = yx["name"].asString();

        std::ostringstream sql;
        sql << "SELECT map_id,name,reg_type,address,is_bit,bit_offset,bit_num,is_filter,flag FROM t_modbus_map_yx";
        sql << " WHERE ";
        sql << "map_id='" << map_id << "'";
        sql << " AND ";
        sql << "name='" << name << "'";

        std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));
        if(!res) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
            return false;
        }

        bool exits = res->next();
        yx["init"] = exits;
        yx["map_id"] = map_id;
        yx["reg_type"] = exits ? res->asInt(2) : -1;
        yx["address"] = exits ? res->asInt(3) : -1;
        yx["is_bit"] = exits ? res->asBool(4) : false;
        yx["bit_offset"] = exits ? res->asInt(5) : -1;
        yx["bit_num"] = exits ? res->asInt(6) : -1;
        yx["is_filter"] = exits ? res->asBool(7) : false;
    }
    return true;
}

bool CSsqlMgr::addModbusMapYcs(const std::string &map_id, Json::Value &mapycs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapycs) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        int datalen = item["datalen"].asInt();
        int datatype = item["datatype"].asInt();

        buf << "INSERT INTO t_modbus_map_yc(map_id,name,reg_type,address,datalen,datatype) VALUES (";
        buf << "'" << map_id << "',";
        buf << "'" << name << "',";
        buf  << reg_type << ",";
        buf << address << ",";
        buf << datalen << ",";
        buf << datatype << ");";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }

    return true;
}

bool CSsqlMgr::changeModbusMapYcs(const std::string &map_id, Json::Value &mapycs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapycs) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        int datalen = item["datalen"].asInt();
        int datatype = item["datatype"].asInt();

        buf << "UPDATE t_modbus_map_yc SET ";
        buf << "reg_type=" << reg_type <<",";
        buf << "address=" << address << ",";
        buf << "datalen=" << datalen << ",";
        buf << "datatype=" << datatype;
        buf << " WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::deleteModbusMapYcs(const std::string &map_id, Json::Value &mapycs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapycs) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        buf << "DELETE FROM t_modbus_map_yc WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";
        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::getModbusMapYcs(const std::string &map_id, Json::Value &mapsigs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    Json::Value& ycs = mapsigs["mapsigs"];

    for(auto& yc : ycs) {
        std::string name = yc["name"].asString();

        std::ostringstream sql;
        sql << "SELECT map_id,name,reg_type,address,datalen,datatype,flag FROM t_modbus_map_yc";
        sql << " WHERE ";
        sql << "map_id='" << map_id << "'";
        sql << " AND ";
        sql << "name='" << name << "'";

        std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));
        if(!res) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
            return false;
        }

        bool exits = res->next();
        yc["init"] = exits;
        yc["map_id"] = map_id;
        yc["reg_type"] = exits ? res->asInt(2) : -1;
        yc["address"] = exits ? res->asInt(3) : -1;
        yc["datalen"] = exits ? res->asInt(4) : -1;
        yc["datatype"] = exits ? res->asInt(5) : -1;
    }
    return true;
}

bool CSsqlMgr::addModbusMapYks(const std::string &map_id, Json::Value &mapyks) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapyks) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        bool is_filter = item["is_filter"].asBool();
        bool is_bit = item["is_bit"].asBool();
        int bit_offest = item["bit_offset"].asInt();
        int bit_num = item["bit_num"].asInt();

        buf << "INSERT INTO t_modbus_map_yk(map_id,name,reg_type,address,is_bit,bit_offset,bit_num,is_filter) VALUES (";
        buf << "'" << map_id << "',";
        buf << "'" << name << "',";
        buf  << reg_type << ",";
        buf << address << ",";
        buf << is_bit << ",";
        buf << bit_offest << ",";
        buf << bit_num << ",";
        buf << is_filter << ");";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }

    return true;
}

bool CSsqlMgr::changeModbusMapYks(const std::string &map_id, Json::Value &mapyks) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapyks) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        bool is_filter = item["is_filter"].asBool();
        bool is_bit = item["is_bit"].asBool();
        int bit_offest = item["bit_offset"].asInt();
        int bit_num = item["bit_num"].asInt();

        buf << "UPDATE t_modbus_map_yk SET ";
        buf << "reg_type=" << reg_type <<",";
        buf << "address=" << address << ",";
        buf << "is_bit=" << is_bit << ",";
        buf << "is_filter=" << is_filter << ",";
        buf << "bit_offset=" << bit_offest << ",";
        buf << "bit_num=" << bit_num;
        buf << " WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::deleteModbusMapYks(const std::string &map_id, Json::Value &mapyks) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapyks) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        buf << "DELETE FROM t_modbus_map_yk WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";
        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::getModbusMapYks(const std::string &map_id, Json::Value &mapsigs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    Json::Value& yks = mapsigs["mapsigs"];
    for(auto& yk : yks) {
        std::string name = yk["name"].asString();

        std::ostringstream sql;
        sql << "SELECT map_id,name,reg_type,address,is_bit,bit_offset,bit_num,is_filter,flag FROM t_modbus_map_yk";
        sql << " WHERE ";
        sql << "map_id='" << map_id << "'";
        sql << " AND ";
        sql << "name='" << name << "'";

        std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));
        if(!res) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
            return false;
        }

        bool exits = res->next();
        yk["init"] = exits;
        yk["map_id"] = map_id;
        yk["reg_type"] = exits ? res->asInt(2) : -1;
        yk["address"] = exits ? res->asInt(3) : -1;
        yk["is_bit"] = exits ? res->asBool(4) : false;
        yk["bit_offset"] = exits ? res->asInt(5) : -1;
        yk["bit_num"] = exits ? res->asInt(6) : -1;
        yk["is_filter"] = exits ? res->asBool(7) : false;
    }
    return true;
}

bool CSsqlMgr::addModbusMapParameters(const std::string &map_id, Json::Value &mapparameters) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapparameters) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        int datalen = item["datalen"].asInt();
        int datatype = item["datatype"].asInt();

        buf << "INSERT INTO t_modbus_map_parameter(map_id,name,reg_type,address,datalen,datatype) VALUES (";
        buf << "'" << map_id << "',";
        buf << "'" << name << "',";
        buf  << reg_type << ",";
        buf << address << ",";
        buf << datalen << ",";
        buf << datatype << ");";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }

    return true;
}

bool CSsqlMgr::changeModbusMapParameters(const std::string &map_id, Json::Value &mapparameters) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapparameters) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        int reg_type = item["reg_type"].asInt();
        int address = item["address"].asInt();
        int datalen = item["datalen"].asInt();
        int datatype = item["datatype"].asInt();

        buf << "UPDATE t_modbus_map_parameter SET ";
        buf << "reg_type=" << reg_type <<",";
        buf << "address=" << address << ",";
        buf << "datalen=" << datalen << ",";
        buf << "datatype=" << datatype;
        buf << " WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";

        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::deleteModbusMapParameters(const std::string &map_id, Json::Value &mapparameters) {
    GET_DB_HANDLE(CConfigs::EPlat);
    for(auto& item : mapparameters) {
        std::ostringstream buf;
        std::string name = item["name"].asString();
        buf << "DELETE FROM t_modbus_map_parameter WHERE ";
        buf << "map_id='" << map_id << "'";
        buf << " AND ";
        buf << "name='" << name << "';";
        if (!db->exec(buf.str().c_str())) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + buf.str()));
            return false;
        }
    }
    return true;
}

bool CSsqlMgr::getModbusMapParameters(const std::string &map_id, Json::Value &mapsigs) {
    GET_DB_HANDLE(CConfigs::EPlat);
    Json::Value& parameters = mapsigs["mapsigs"];

    for(auto& parameter : parameters) {
        std::string name = parameter["name"].asString();

        std::ostringstream sql;
        sql << "SELECT map_id,name,reg_type,address,datalen,datatype,flag FROM t_modbus_map_parameter";
        sql << " WHERE ";
        sql << "map_id='" << map_id << "'";
        sql << " AND ";
        sql << "name='" << name << "'";

        std::shared_ptr<CResultSet> res(db->query(sql.str().c_str()));
        if(!res) {
            OUTPUT_WARNING_INFO(std::string("db exec failed: " + sql.str()));
            return false;
        }

        bool exits = res->next();
        parameter["init"] = exits;
        parameter["map_id"] = map_id;
        parameter["reg_type"] = exits ? res->asInt(2) : -1;
        parameter["address"] = exits ? res->asInt(3) : -1;
        parameter["datalen"] = exits ? res->asInt(4) : -1;
        parameter["datatype"] = exits ? res->asInt(5) : -1;
    }
    return true;
}












